﻿package ui.control
{
    import flash.display.GradientType;
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.filters.DropShadowFilter;
    import flash.geom.Matrix;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFieldType;
    import flash.text.TextFormat;
   
    public class Button extends MovieClip
    {
        /** Dafault value of height **/
        protected const DEFAULT_HEIGHT: Number = 22;
        /** Default value of width **/
        protected const DEFAULT_WIDTH: Number = 100;
        /** Width **/
        protected var _width: Number;
        /** Height **/
        protected var _height: Number;
        /** Field base for Caption property **/
        protected var _text:TextField = new TextField
        
        protected var reactArea: MovieClip = new MovieClip
        
        /** Rounded corner for button **/
        public var roundCorner: Number = 0
        
        
        /** Constructor
         * @param caption text on buttonMode
         * @param autoSize ability change widht and heithgt parameter of buttonMode
         * @param w width
         * @param h height
         */
        public function Button(caption:String = "", autoSize:Boolean=true, w:Number=DEFAULT_WIDTH, h:Number=DEFAULT_HEIGHT)
        {
            super()
            // text field
            _text.selectable = false;
            _text.type = TextFieldType.DYNAMIC;
            _text.autoSize = TextFieldAutoSize.LEFT;
            _text.multiline = false;
            _text.defaultTextFormat = new TextFormat("_sans", "12", 0xFFFFFF, true);
            _text.text = caption;
            _text.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
            _text.x = 10
            addChild(_text)
            if (autoSize) 
            {
                _width = _text.width + 20;
                _height = _text.height;
            } 
            else 
            {
                _width = w;
                _height = h;
                _text.width = w;
                _text.height = h;
            }
            addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler)
        }
        
        
        public function addedToStageHandler(event:Event):void
        {
            drawUpButton();
            drawReactArea();
        }
        
        /** Property setter on _text field **/
        public function set caption (value:String): void 
        {
            _text.text = value;
        }
        
        /** Property getter on _text field **/
        public function get caption (): String 
        {
            return _text.text;
        }
        
        /** Draw "UP" state for button **/
        private function drawUpButton (): void 
        {
            graphics.clear();
            graphics.beginFill(0xFFFFFF, 0.3);
            graphics.drawRoundRect(3, 3, _width-3, _height-3, roundCorner, roundCorner);
            graphics.endFill();
        }
        
        /** Draw "Over" state for button **/
        private function drawOverButton (): void 
        {
            var matr: Matrix = new Matrix();
            matr.createGradientBox(100, 40, Math.PI/2, -25, -25);
            graphics.clear();
            //graphics.beginGradientFill(GradientType.LINEAR, [0x74CCF1, 0x0B5EB0], [1.0, 1.0], [100, 255], matr)
            graphics.beginGradientFill(GradientType.LINEAR, [0xF4A4D5, 0xFF3385], [1.0, 1.0], [100, 255], matr); // povod colors
            graphics.drawRoundRect(0, 0, _width, _height, roundCorner, roundCorner);
            graphics.endFill();
        }
        
        /**  **/    
        private function drawReactArea (): void 
        {
            reactArea.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler)
            reactArea.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler)
            reactArea.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler)
            reactArea.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler)
            
            reactArea.buttonMode = true
            reactArea.useHandCursor = true
            reactArea.visible = true
            reactArea.graphics.beginFill(0xFF0000, 0.0)
            reactArea.graphics.drawRoundRect(2, 2, _width-2, _height-2, roundCorner, roundCorner)
            reactArea.graphics.endFill()
            addChild(reactArea)    
        }
        
        private function mouseDownHandler(event:MouseEvent):void 
        {
            y += 1
            drawOverButton()
            filters = [new DropShadowFilter(0, 90, 0x000000, 0.7, 4, 4)]
        }
        
        private function mouseUpHandler(event:MouseEvent):void 
        {
            y -= 1
            drawOverButton()
            filters = [new DropShadowFilter(2, 90, 0x000000, 0.7, 4, 4)]         
        }
        
        private function mouseOverHandler(event:MouseEvent):void 
        {
            drawOverButton()
            _text.textColor = 0xFFFFFF
            filters = [new DropShadowFilter(2, 90, 0x000000, 0.7, 4, 4)]
        }

        private function mouseOutHandler(event:MouseEvent):void 
        {
            drawUpButton()
            _text.textColor = 0xFFFFFF
            filters = []
        }

   }
}